package com.bs.jianli.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bs.jianli.entity.Menu;
import com.bs.jianli.service.IMenuService;
import com.bs.jianli.service.IRolemenuService;
import com.bs.jianli.util.Result;
import com.bs.jianli.util.auto.Auth;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Update;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 菜单管理表 前端控制器
 * </p>
 *
 * @author yao
 * @since 2023-01-13
 */
@RestController
@RequestMapping("/menu")
@Api(tags = "菜单系统管理")
public class MenuController {

    @Resource
    IMenuService service;



    @ApiOperation("菜单列表")
    @GetMapping
    @Auth("system:menu")
    @PreAuthorize("@auth.getAuth('system:menu')")
    public Result index(@RequestParam(defaultValue = "1") @ApiParam("当前页") Integer pageNum, @RequestParam(defaultValue = "5")  @ApiParam("页面大小") Integer pageSize, @RequestParam(defaultValue = "") @ApiParam("菜单名") String name){
        Page<Menu> page=new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<Menu> lq=new LambdaQueryWrapper<>();
        lq.like(StringUtils.isNotBlank(name),Menu::getMenuname,name);
        page=service.page(page,lq);
        return Result.success(page);
    }

    @ApiOperation("添加菜单")
    @PostMapping
    public Result add(@RequestBody @ApiParam("菜单表") Menu menu){
        boolean save = service.save(menu);
        if(save){
            return Result.success("添加成功");
        }else{
            return Result.success("添加失败");

        }
    }

    @ApiOperation("根据菜单id查询单个菜单")
    @GetMapping("/{id}")
    public Result getByMenu(@PathVariable("id") @ApiParam("菜单id") String id){
        Menu menu = service.getById(id);
        return Result.success(menu);
    }

    @ApiOperation("菜单修改")
    @PutMapping
    public Result update(@RequestBody @ApiParam("菜单表") Menu menu){
        Menu menu1 = service.getById(menu.getMenuid());
        menu.setVersion(menu1.getVersion());
        boolean update = service.updateById(menu);
        if(update){
            return Result.success("修改成功");
        }else{
            return Result.success("修改失败");

        }
    }


    @ApiOperation("菜单删除")
    @DeleteMapping("/{id}")
    public Result del(@PathVariable("id") @ApiParam("菜单id") String id){
        boolean del = service.removeById(id);
        if(del){
            return Result.success("删除成功");
        }else{
            return Result.success("删除失败");

        }
    }


    @GetMapping("/getByTree")
    public Result getByPid(@RequestParam(defaultValue = "0") String pid){
        List<Menu> menus = service.getByPid(pid);
        return Result.success(menus);
    }


}

